package com.astoolkit.onair.workflow
{
	import com.astoolkit.workflow.BaseTask;
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	
	import com.astoolkit.onair.vo.AirModuleConfig;
	
	import newloop.air.moduleloading.PackageSignatureCheck;
	
	public class CheckModuleSignatureTask extends BaseTask
	{
		public var config : AirModuleConfig;
		
		override public function begin():void
		{
			super.begin();
			config = _pipelineData;
			setProgress( 0 );
			var packageChecker : PackageSignatureCheck = new PackageSignatureCheck();
			packageChecker.addEventListener( PackageSignatureCheck.PACKAGE_SIGNATURE_PASSED, onSignatureCheckPassed );
			packageChecker.addEventListener( PackageSignatureCheck.PACKAGE_SIGNATURE_FAILED, onSignatureCheckFailed );
			packageChecker.addEventListener( PackageSignatureCheck.PACKAGE_SIGNATURE_ERROR, onSignatureCheckError );
			packageChecker.addEventListener( PackageSignatureCheck.READY, onSignatureCheckerReady );
			packageChecker.addEventListener( ErrorEvent.ERROR, onPackageCheckerError );
			packageChecker.initialise();
			
		}
		
		private function onPackageCheckerError( inEvent : ErrorEvent ) : void
		{
			fail( "Package check error" );
		}
		
		private function onSignatureCheckPassed( inEvent : Event ) : void
		{
			complete();
		}
		
		private function onSignatureCheckFailed( inEvent : Event ) : void
		{
			fail( "Signature check failed" );
		}
		
		private function onSignatureCheckError( inEvent : ErrorEvent ) : void
		{
			fail( "Signature check error" );
		}
		
		private function onSignatureCheckerReady( inEvent : Event ) : void
		{
			var mySignatureChecker : PackageSignatureCheck = 
				inEvent.target as PackageSignatureCheck;
			mySignatureChecker.validate( config.localRepository.nativePath );
		}
		
	}
}